package com.hq.shop06back.controller;

import com.hq.commons.common.R;
import com.hq.commons.exception.vo.BusinessException;
import com.hq.shop06back.feign.api.UserService;
import com.hq.shop06back.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/userController")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page) {
        return "user/" + page;
    }

    @PostMapping("/addUser")
    @ResponseBody
    public R addUser(User user) {

        boolean b = userService.addUser(user);
        if (user.getSex() == 1) {
            throw new BusinessException("1002", "该系统只允许女性注册");
        }
        return R.write(b);
    }

    @RequestMapping("getList")
    public String getList(User user, ModelMap modelMap) {
        List<User> userList = userService.queryList(user);
        modelMap.put("userList", userList);
        return "user/userList";
    }
}
